package com.fdb.efp.nls.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.fdb.efp.nls.service.domain.NlsLoanApprovalInfo;
import com.fdb.efp.nls.service.facade.NlsLoanApprovalInfoService;
import com.fdb.efp.nls.service.vo.NlsLoanApprovalInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.NlsLoanApprovalInfoDao;

/**
 * 贷款进件审批清单
 * 
 * @author wanglh
 * 
 * @version 1.0
 */
@Service("nlsLoanApprovalInfoService")
public class NlsLoanApprovalInfoServiceImpl implements NlsLoanApprovalInfoService, FrameworkService {

private static Logger logger = LoggerFactory.getLogger(NlsLoanApprovalInfoServiceImpl.class);
	
	@Autowired
	private NlsLoanApprovalInfoDao nlsLoanApprovalInfoDao;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<NlsLoanApprovalInfoVO> querynlsLoanApprovalInfoByPage(NlsLoanApprovalInfoVO nlsLoanApprovalInfoVO, String dataRuleType) {
		List<NlsLoanApprovalInfoVO> list = new ArrayList<NlsLoanApprovalInfoVO>();//定义用来返回查询结果的list
		try {
			handleDateRange(nlsLoanApprovalInfoVO);
			NlsLoanApprovalInfo queryNlsLoanApprovalInfos = new NlsLoanApprovalInfo();
			beanCopy(nlsLoanApprovalInfoVO, queryNlsLoanApprovalInfos);
			List<NlsLoanApprovalInfo> queryRsNlsLoanApprovalInfoByPage = this.dataRuleFilterByPage(queryNlsLoanApprovalInfos, dataRuleType);
			pageSet(queryRsNlsLoanApprovalInfoByPage, queryNlsLoanApprovalInfos);
			list = (List<NlsLoanApprovalInfoVO>) beansCopy(queryRsNlsLoanApprovalInfoByPage, NlsLoanApprovalInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		logger.info("查到的数据>>>" + list.toString());
		return list;
	}
	
	private void handleDateRange(NlsLoanApprovalInfoVO nlsLoanApprovalInfoVO) {
		//对时间区间的处理
		if(nlsLoanApprovalInfoVO.getApplyDate_b2e() != null && nlsLoanApprovalInfoVO.getApplyDate_b2e().toString() !="[]") {
			logger.info(nlsLoanApprovalInfoVO.getApplyDate_b2e().toString());
			nlsLoanApprovalInfoVO.setApplyDate_b(nlsLoanApprovalInfoVO.getApplyDate_b2e().get(0));
			nlsLoanApprovalInfoVO.setApplyDate_e(nlsLoanApprovalInfoVO.getApplyDate_b2e().get(1));
		}
	}

	/**
	  * 根据权限方案调用不同的方案进行数据查询
	  * @param queryNlsLoanApprovalInfos
	  * @param dataRuleType
	  * @return
	  * @author qiuyf
	  * @since 2019年2月12日 下午6:10:15
	  * @version 1.0
	 */
	private List<NlsLoanApprovalInfo> dataRuleFilterByPage(NlsLoanApprovalInfo queryNlsLoanApprovalInfos, String dataRuleType){
		List<NlsLoanApprovalInfo> result = null;
		switch (dataRuleType) {
		case "01":
			result = nlsLoanApprovalInfoDao.queryAllOwnerByPage(queryNlsLoanApprovalInfos);
			break;
		case "02":
			result = nlsLoanApprovalInfoDao.queryAllCurrOrgByPage(queryNlsLoanApprovalInfos);
			break;
		case "03":
			result = nlsLoanApprovalInfoDao.queryAllCurrDownOrgByPage(queryNlsLoanApprovalInfos);
			break;
		case "04":
			result = nlsLoanApprovalInfoDao.queryAllCurrOwnerPrdByPage(queryNlsLoanApprovalInfos);
			break;
		default:
			result = nlsLoanApprovalInfoDao.queryAllOwnerByPage(queryNlsLoanApprovalInfos);
			break;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<NlsLoanApprovalInfoVO> querynlsLoanApprovalInfo(NlsLoanApprovalInfoVO nlsLoanApprovalInfoVO, String dataRuleType) {
		List<NlsLoanApprovalInfoVO> list = new ArrayList<NlsLoanApprovalInfoVO>();
		try {
			handleDateRange(nlsLoanApprovalInfoVO);
			NlsLoanApprovalInfo queryNlsLoanApprovalInfos = new NlsLoanApprovalInfo();
			beanCopy(nlsLoanApprovalInfoVO, queryNlsLoanApprovalInfos);
			List<NlsLoanApprovalInfo> queryRsNlsLoanApprovalInfo = this.dataRuleFilter(queryNlsLoanApprovalInfos, dataRuleType);
			list = (List<NlsLoanApprovalInfoVO>) beansCopy(queryRsNlsLoanApprovalInfo, NlsLoanApprovalInfoVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}

	private List<NlsLoanApprovalInfo> dataRuleFilter(NlsLoanApprovalInfo queryNlsLoanApprovalInfos, String dataRuleType) {
		List<NlsLoanApprovalInfo> result = null;
		switch (dataRuleType) {
		case "01":
			result = nlsLoanApprovalInfoDao.queryAllOwner(queryNlsLoanApprovalInfos);
			break;
		case "02":
			result = nlsLoanApprovalInfoDao.queryAllCurrOrg(queryNlsLoanApprovalInfos);
			break;
		case "03":
			result = nlsLoanApprovalInfoDao.queryAllCurrDownOrg(queryNlsLoanApprovalInfos);
			break;
		case "04":
			result = nlsLoanApprovalInfoDao.queryAllCurrOwnerPrd(queryNlsLoanApprovalInfos);
			break;
		default:
			result = nlsLoanApprovalInfoDao.queryAllOwner(queryNlsLoanApprovalInfos);
			break;
		}
		return result;
	}
}